package linter.linterjava.controller.note;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.KnowledgeBase;
import linter.linterjava.POJO.DO.Note;
import linter.linterjava.POJO.DO.NoteKnowledgebase;
import linter.linterjava.POJO.DTO.NoteKnowledgebaseDTO;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.POJO.UtilDTO.MoveUtilDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.NoteKnowledgebaseService;
import linter.linterjava.service.NoteService;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@ApiOperation("知识库模块")
@RestController
@RequestMapping("knowledgeBase")
public class KnowledgeBaseController {

    @Resource
    UserService userService;
    @Resource
    NoteKnowledgebaseService noteKnowledgebaseService;
    private final UserQueryService userQueryService;
    @Autowired
    public KnowledgeBaseController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }

    /**
     * @param knowledgeBase 知识库笔记中间类 -- 需要知识库id、笔记id
     */
    @PostMapping("noteGroup")
    @ApiOperation("进行笔记分组")
    public ResultVO<String> noteGroup(@RequestBody KnowledgeBase knowledgeBase){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.group(knowledgeBase, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("delNoteGroup")
    @ApiOperation("删除笔记分组 -- 整体")
    public ResultVO<String> delNoteGroup(@RequestParam Long noteKnowledgeBaseId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.delNoteGruop(noteKnowledgeBaseId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("delNoteGroupItem")
    @ApiOperation("删除笔记分组 -- 单个")
    public ResultVO<String> delNoteGroupItem(@RequestParam Long noteKnowledgeBaseId, @RequestParam Long noteId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.delNoteGruopItem(noteKnowledgeBaseId, noteId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param moveUtilDTO 移动工具类 -- 分组新id，旧id
     */
    @PostMapping("moveNote")
    @ApiOperation("移动笔记到其他分组 -- 整体")
    public ResultVO<String> moveNote(@RequestBody MoveUtilDTO moveUtilDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.moveNote(moveUtilDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("createGroup")
    @ApiOperation("创建笔记分组")
    public ResultVO<String> createGroup(@RequestBody NoteKnowledgebaseDTO noteKnowledgebaseDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.createGroup(noteKnowledgebaseDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param pageInfoDTO 分页类 -- pageSize、pageNum
     */
    @PostMapping("selectGroup")
    @ApiOperation("查询笔记分组 -- 所有")
    public ResultVO<PageInfo<NoteKnowledgebase>> selectGroup(@RequestBody PageInfoDTO pageInfoDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.selectGroup(pageInfoDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param knowledgeBaseDTO 知识库类 -- pageSize、pageNum、知识库id
     */
    @PostMapping("selectGroupById")
    @ApiOperation("查询笔记分组 -- 知识库id")
    public ResultVO<PageInfo<Note>> selectGroupByName(@RequestBody NoteKnowledgebaseDTO knowledgeBaseDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.selectGroupById(knowledgeBaseDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param knowledgeBaseDTO 知识库类 -- 需要知识库名称、知识库id
     */
    @PostMapping("updateGroupName")
    @ApiOperation("修改知识库名字")
    public ResultVO<String> updateGroupName(@RequestBody NoteKnowledgebaseDTO knowledgeBaseDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteKnowledgebaseService.updateGroupName(knowledgeBaseDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

}
